package com.dongzili.starrocks.custom.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongzili.starrocks.common.config.mybatisplus.MybatisPlusUtil;
import com.dongzili.starrocks.common.vo.QueryRequest;
import com.dongzili.starrocks.custom.entity.Relation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/relation")
public class RelationController {


    @PostMapping("/add")
    public Object add(@RequestBody Relation data){
        return MybatisPlusUtil.getService(Relation.class).saveOrUpdate(data);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody Relation data){
        return MybatisPlusUtil.getService(Relation.class).removeById(data.getId());
    }

    @PostMapping("/list")
    public List<Relation> getList(@RequestBody QueryRequest<Relation> request){
        LambdaQueryWrapper<Relation> wrapper = new LambdaQueryWrapper<>(request.getEntity());
        if (request.getLikeEntity() != null && request.getLikeEntity().getResName() != null){
            wrapper.like(Relation::getResName,request.getLikeEntity().getResName());
        }
        return MybatisPlusUtil.getService(Relation.class).list(new Page<>(request.getCurrent(), request.getSize()),wrapper);
    }
}
